package com.cdtye.itps.cms.service.datasysn;

import com.cdtye.common.websocketcore.config.WebSocketCore;
import com.cdtye.common.websocketcore.handler.WsHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.stereotype.Service;

import javax.websocket.Session;

@Slf4j
@Service
public class WsHandlerImpl implements WsHandler {

    public void saveHandleWsMsg(ConsumerRecord record) {
        try {
            if (StringUtils.isNotBlank((String) record.key())) {
                WebSocketCore.pushToUser((String) record.key(), record.value().toString());
            } else {
                //后续扩展通过实体来判断推送方式
                WebSocketCore.pushToAll(record.value().toString());
            }
        } catch (Exception e) {
            log.error("kafka消费websocket消息推送异常：" + e.getMessage(), e);
        }
    }
    /**
     * 接收websocket消息处理,不用try catch处理
     *
     * @param session
     * @param message
     */
    @Override
    public void onMessage(Session session, String message) {

    }
}
